<%@ page pageEncoding="UTF-8"%>
<%@ include file="/common/token.jsp"%>
<%@ include file="/common/message.jsp"%>
<%
    Integer id = input.getInt("schedule_id", -1);   // 排班id
    Integer pid = input.getInt("pid", -1);          // 就这人id
    String startTime = input.getDesc("start_time", "");   // 开始时间
    String endTime = input.getDesc("end_time", "");     // 结束时间
    List<Schedule> shcedules = QBuilder.db("basedb").select().from("Schedule").where(new Where("status = ", STATUS_ENABLE).and("id=", id)).query().getResultObj();
    if (shcedules.size() == 0) {
        msg = new Message(30041, "号源失效");
        Tools.encryptPrint(out, msg);
        return;
    }
    List<String> names = QBuilder.db("basedb").select("name").from("UserProfile").where(new Where("status = ", STATUS_ENABLE).and("id=", pid).and("uid=", token.getUid())).query().getResultObj();
    if (isEmpty(names)) {
        msg = new Message(30041, "用户信息错误");
        Tools.encryptPrint(out, msg);
        return;
    }
    Schedule schedule = shcedules.get(0);
	hospital = schedule.getHospital();
    List<Map> orders = QBuilder.db("basedb").select("new map(id as register_id, patientName as patient_name, doctorName as doctor_name, deptName as dept_name, date as date, half as half, registerStatus as register_status, branch as branch, jsonString as json)").from("RegisterOrder").where(new Where("hospital=", hospital).and("status=", STATUS_ENABLE).and("userProfileId =", pid).and("date=", schedule.getDate())).query().getResultObj();
    if (orders.size() > 0) {
        msg = new Message(30041, "该用户已经预约当天号源,请勿重复预约");
        Tools.encryptPrint(out, msg);
        return;
    }
    RegisterOrder registerOrder = new RegisterOrder();
    registerOrder.setHospital(hospital);
    registerOrder.setRegisterStatus(1);
    registerOrder.setUserProfileId(pid);
    registerOrder.setPatientName(names.get(0));
    registerOrder.setDeptId(schedule.getDeptId());
    registerOrder.setDeptName(schedule.getDeptName());
    registerOrder.setDoctorId(schedule.getDoctorId());
    registerOrder.setDoctorName(schedule.getDoctorName());
    registerOrder.setDate(schedule.getDate());
    registerOrder.setHalf(schedule.getHalf()+"");
    registerOrder.setBranch(schedule.getBranch());
    registerOrder.setJson("start_time", startTime);
    registerOrder.setJson("end_time", endTime);
    //registerOrder.setOrderId();
    id = (Integer)DBTools.db("basedb").addItem(registerOrder);
    if (isEmpty(id)) {
        msg = new Message(30031, "预约失败");
    } else {
        msg.put("data", id);
        createMsg(registerOrder, token.getUid());
    }
    Tools.encryptPrint(out, msg);
%>
